package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户端-购物车")
@Slf4j
@RequestMapping("/user/shoppingCart")
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;



    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车：{}",shoppingCartDTO);
        //调用业务层处理逻辑
        shoppingCarService.addShoppingCar(shoppingCartDTO);

        return Result.success();
    }



    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list(){
        //调用服务层处理逻辑
      List<ShoppingCart> shoppingCartList = shoppingCarService.showShoppingCart();
        return Result.success(shoppingCartList);
    }



    @DeleteMapping ("/clean")
    @ApiOperation("清空购物车")
    public Result clean(){
        //访问业务层，完成清空逻辑
        shoppingCarService.cleanShoppingCart();
        return Result.success();
    }



    @PostMapping("/sub")
    @ApiOperation("删除购物车中的一个商品")
    public Result deleteById(@RequestBody ShoppingCartDTO shoppingCartDTO){
            shoppingCarService.deleteById(shoppingCartDTO);
        return Result.success();
    }

}
